"""
    学习练习openpyxl操作excel表格
    官网地址：https://openpyxl.readthedocs.io/en/stable/tutorial.html
"""
# 加载已有的excel模块
from openpyxl import load_workbook
# 创建表格模块
from openpyxl import Workbook
# 为了支持jpeg, png, bmp等图片格式，需要安装 pillow 模块，pip install pillow
from openpyxl.drawing.image import Image
import datetime


def load_excel():
    """
    加载表格
    """
    print('-----load excel----')
    wb = load_workbook('test.xlsx')
    # 所有的sheet
    sheets = wb.sheetnames
    print(f'所有的sheet:{sheets}')
    for sheet in wb:
        print(sheet.title)


def create_excel():
    """
    创建表格
    """
    wb = Workbook()
    # 获取默认的sheet，第一个sheet
    ws = wb.active
    # 直接给指定单元格赋值
    ws['A1'] = 42
    # 给单元格追加数值
    ws.append([1, 2, 3])
    ws["A3"] = datetime.datetime.now()
    wb.save('sample.xlsx')


def create_img_excel():
    wb = Workbook()
    ws = wb.active
    ws['A1'] = 'You should see three logos below'
    # 创建一张图片
    img = Image('model_1.jpg')
    ws.add_image(img, 'A1')
    wb.save('log.xlsx')


if __name__ == '__main__':
    # load_excel()
    # create_excel()
    create_img_excel()
